package com.example.telesys.controller;

import com.example.telesys.pojo.*;
import com.example.telesys.service.CustomerService;
import com.example.telesys.service.CustomerorderService;
import com.example.telesys.service.SupplierService;
import com.example.telesys.utils.DateUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;

@RestController
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private CustomerService customerService;

    @Autowired
    private CustomerorderService customerorderService;

    @Autowired
    private SupplierService supplierService;

    @RequestMapping("/selectAllCustomers")
    public ApiResponse selectAllCustomers(
            @RequestParam("pageNumber") Integer pageNumber,
            @RequestParam("pageSize") Integer pageSize) {
        PageInfo pageInfo = customerService.selectAllCustomers(pageNumber, pageSize);
        return new ApiResponse(StatusCode.SUCCESS, StatusMessage.SUCCESS, pageInfo);
    }

    @RequestMapping("/selectCustomersByName")
    public ApiResponse selectCustomersByName(
            @RequestParam("customername") String customername,
            @RequestParam("pageNumber") Integer pageNumber,
            @RequestParam("pageSize") Integer pageSize) {
        PageInfo pageInfo = customerService.selectCustomersByName(customername, pageNumber, pageSize);
        return new ApiResponse(StatusCode.SUCCESS, StatusMessage.SUCCESS, pageInfo);
    }

    @RequestMapping("/selectAllCustomerOrders")
    public ApiResponse selectAllCustomerOrders(
            @RequestParam("pageNumber") Integer pageNumber,
            @RequestParam("pageSize") Integer pageSize) {
        PageInfo pageInfo = customerorderService.selectAllCustomerOrders(pageNumber, pageSize);
        return new ApiResponse(StatusCode.SUCCESS, StatusMessage.SUCCESS, pageInfo);
    }

    @RequestMapping("/selectAllCustomerData")
    public ApiResponse selectAllCustomerData(
            @RequestParam("pageNumber") Integer pageNumber,
            @RequestParam("pageSize") Integer pageSize) {
        //1.找出所有的客户 并统计出对应的总量
        List<Customerorder> customerorders = customerorderService.selectCustomerAndNumber(pageNumber, pageSize).getList();
        return new ApiResponse(StatusCode.SUCCESS, StatusMessage.SUCCESS, customerorders);
    }

    public int getMonthByString(String date) {
        String monthStr = date.substring(5, 7);
        if (monthStr.startsWith("0")) {
            char monthChar = monthStr.charAt(1);
            return Integer.parseInt(String.valueOf(monthChar));
        } else {
            return Integer.parseInt(monthStr);
        }
    }

    @RequestMapping("/selectYearlyOrder")
    public ApiResponse selectYearlyOrder(@RequestParam("username") String username) {
        //本年度
        String today = DateUtil.getToday();
        String year = today.substring(0, 4);
        List<Customerorder> customerorders = customerorderService.selectYearlyOrder(username, year);
        double years[] = new double[12];
        if (customerorders.size() != 0) {
            for (Customerorder customerorder : customerorders) {
                String date = customerorder.getDate();
                int month = getMonthByString(date);
                if (years[month - 1] != 0) {
                    years[month - 1] += customerorder.getNumber();
                } else {
                    years[month - 1] = customerorder.getNumber();
                }
            }
        }
        return new ApiResponse(StatusCode.SUCCESS, StatusMessage.SUCCESS, years);
    }

    @RequestMapping("/selectSupplierData")
    public ApiResponse selectSupplierData(
            @RequestParam("pageNumber") Integer pageNumber,
            @RequestParam("pageSize") Integer pageSize) {
        PageInfo pageInfo = supplierService.selectSupplierData(pageNumber, pageSize);
        return new ApiResponse(StatusCode.SUCCESS, StatusMessage.SUCCESS, pageInfo);
    }


    @RequestMapping("/setOrderStatus")
    public ApiResponse setOrderStatus(
            @RequestParam("role") String role,
            @RequestParam("orderid") String orderid,
            @RequestParam("code") Integer code) {
        int setOrderStatus = customerorderService.setOrderStatus(role, orderid, code);
        if (setOrderStatus == 0)
            return new ApiResponse(StatusCode.FAILED, StatusMessage.FAILED, setOrderStatus);
        return new ApiResponse(StatusCode.SUCCESS, StatusMessage.SUCCESS, setOrderStatus);
    }

    @RequestMapping("/selectCustomerOrdersByOrderId")
    public ApiResponse selectCustomerOrdersByOrderId(
            @RequestParam("orderid") String orderid,
            @RequestParam("pageNumber") Integer pageNumber,
            @RequestParam("pageSize") Integer pageSize) {
        PageInfo pageInfo = customerorderService.selectCustomerOrdersByOrderId(orderid, pageNumber, pageSize);
        return new ApiResponse(StatusCode.SUCCESS, StatusMessage.SUCCESS, pageInfo);
    }

}


